package com.yxjx.duoxue;

import java.util.ArrayList;


import com.thirdparty.neutra.autoscrollloopviewpager.AutoScrollLoopViewPager;
import com.thirdparty.trinea.autoscrollviewpager.AutoScrollViewPager;
import com.yxjx.duoxue.activities.ActivityIntroductionActivity;
import com.yxjx.duoxue.course.CourseIntroductionActivity;
import com.yxjx.duoxue.customview.InnerScrollView;
import com.yxjx.duoxue.customview.InnerWebView;
import com.yxjx.duoxue.customview.OutScrollView;
import com.yxjx.duoxue.datamodel.DMAdItem;
import com.yxjx.duoxue.network.NwUtils;
import com.yxjx.duoxue.utils.UIHelper;
import com.yxjx.duoxue.utils.Utils;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLayoutChangeListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class TestActivity extends BaseActionBarActivity {
	private OutScrollView mOutScroll;
	private AutoScrollLoopViewPager pagers;
	private LinearLayout tabs;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test_scroll);

		mOutScroll = (OutScrollView) findViewById(R.id.outer);

		pagers = (AutoScrollLoopViewPager) findViewById(R.id.pagers);
		pagers.setAdapter(new AdViewPagerAdapter());
		tabs = (LinearLayout) findViewById(R.id.head_tabs);
	}

	@Override
	protected void onResume() {
		super.onResume();
		// pagers的高度应该为outer的高度减去tabs的高度
		pagers.post(new Runnable() {
			@Override
			public void run() {
				int outHeight = mOutScroll.getHeight();
				int tabHeight = tabs.getHeight();
				int innerWidth = pagers.getWidth();
				pagers.getLayoutParams().width = innerWidth;
				pagers.getLayoutParams().height = outHeight - tabHeight;
				Utils.logd("outHeight = " + outHeight + ",tabHeight = " + tabHeight);
				Utils.logd("width = " + pagers.getLayoutParams().width + ",height = "
						+ pagers.getLayoutParams().height);
				pagers.invalidate();
			}
		});
	}
	
	class AdViewPagerAdapter extends PagerAdapter {

		private ArrayList<View> viewList = new ArrayList<View>();
		private ArrayList<DMAdItem> mDataList = new ArrayList<DMAdItem>();

		public AdViewPagerAdapter() {
			mDataList.add(new DMAdItem(null, 0, 0));
			mDataList.add(new DMAdItem(null, 0, 0));
			mDataList.add(new DMAdItem(null, 0, 0));

			View item = LayoutInflater.from(TestActivity.this).inflate(
					R.layout.segment_h5, null);
			InnerWebView innerScroll = (InnerWebView) item
					.findViewById(R.id.inner);
			innerScroll.parentScrollView = mOutScroll;
			innerScroll.loadUrl("http://www.learnmore.com.cn/m/school_des.html?id=110");
			innerScroll.getSettings().setJavaScriptEnabled(true);
			innerScroll.getSettings().setBuiltInZoomControls(false);
			viewList.add(item);

			item = LayoutInflater.from(TestActivity.this).inflate(
					R.layout.test_page_2, null);
			InnerScrollView innerScroll1 = (InnerScrollView) item.findViewById(R.id.inner);
			innerScroll1.parentScrollView = mOutScroll;
			viewList.add(item);
		}

		@Override
		public int getCount() {
			return viewList.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			position = position % (viewList.size());

			try {
				container.removeView(viewList.get(position));
			} catch (Exception e) {
				// walk round for VIewpaeger.
			}
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			position = position % (viewList.size());
			try {
				container.addView(viewList.get(position));
			} catch (Exception e) {
				// walk round for VIewpaeger.
			}
			return viewList.get(position);
		}
	}
}
